<?php
/*
Cogumelo v0.1 - Map-Experience
Copyright (C) 2008 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@map-experience.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

require_once("Classes/Model/Facade/UseradminFacade.inc");
require_once("Classes/Model/VO/UseradminVO.inc");
require_once("Classes/Controllers/Data/DataController.inc");

//
//
// Useradmin Controller Class
//
//
class  UseradminController extends DataController
{
	private $data;
	
	//
	//	class constructor, creates useradminFacade object
	//
	function __construct()
	{	
		parent::__construct();
		$this->data = new useradminFacade();				
	}
	
	////////////////////////////////////////////
	////////// Find And List Methods //////////
	//////////////////////////////////////////
	
	//
	//	Find by ID
	//
	function Find($id) 
	{
		$data = $this->data->Find($id);
		
		if($data) $this->logcontrol->addLogLine("UserAdmin with ID=".$id." FOUND", 3);
		else $this->logcontrol->addLogLine("UserAdmin with ID=".$id." NOT FOUND", 3);
		
		return $data;
	}
	
	//
	//	Find by Login
	//
	function FindByLogin($login)
	{
		$data = $this->data->FindByLogin($login);
		
		if($data) $this->logcontrol->addLogLine("UserAdmin with Login=".$login." FOUND", 3);
		else $this->logcontrol->addLogLine("UserAdmin with Login=".$login." NOT FOUND", 3);
		
		return $data;
	}
	
	
	//
	// List  Useradmins
	//
	function ListUseradmins($RANGE, $ORDER, $filter = null)
	{
		$data = $this->data->ListUseradmins($filter, $RANGE, $ORDER);
		if($data) $this->logcontrol->addLogLine("UserAdmin list Generated", 3);
		else $this->logcontrol->addLogLine("EMPTY UserAdmin list Generated", 3);
		
		return $data;
	}
	//
	// Count Useradmins (Rows number)
	function ListUseradminsCount($filter)
	{
		$data = $this->data->ListUseradminsCount($filter);
		
		$this->logcontrol->addLogLine("Counted ".$data." Useradmins", 3);
				
		return $data;
	}

	
	////////////////////////////////////////////
	/////////////  Input Methods //////////////
	//////////////////////////////////////////
	
	
	//
	//	Create new useradmin
	//
	function Create($useradmin)		
	{
						
		$newuseradmin = new UseradminVO(null,	
							$useradmin["useradmin_login"],	
							$useradmin["useradmin_passwd"],	
							$useradmin["useradmin_name"],												
							"",
							0,
							$useradmin["useradmin_time_lastchangepasswd"],
							"",
							"",
							""
							);
									
		$data = $this->data->Create($newuseradmin);
		
		if($data) $this->logcontrol->addLogLine("Useradmin created with login=".$useradmin["login"]." and ID=".$data, 3);
		else $this->logcontrol->addLogLine("Unable to create Useradmin with login=".$useradmin["login"], 3);
		
		return $data;
		
	}
	
	
    //
	//	Update useradmin data (no passwd update)
	//
	function Update($useradmin)		
	{
						
		$newuseradmin = new UseradminVO(
							$useradmin["useradmin_id"],
							$useradmin["useradmin_login"],								
							"",
							$useradmin["useradmin_name"],
							null , 
							0,
							null,
							"",
							"",
							""
						);

									
		$data = $this->data->Update($newuseradmin);
		
		if($data) $this->logcontrol->addLogLine("Useradmin updated with login=".$useradmin["login"]." and ID=".$data, 3);
		else $this->logcontrol->addLogLine("Unable to update Useradmin with login=".$useradmin["login"], 3);
		
		return $data;
		
	}
	
	
	//
	//	Update Useradmin passwd.
	//
	function UpdatePasswd($newupdateadmin)		
	{
		
		$newuseradmin = new UseradminVO(
							$newupdateadmin["useradmin_id"],
							"",
							$newupdateadmin["useradmin_passwd"],
							"",
							0,
							"",
							$newupdateadmin["useradmin_time_lastchangepasswd"],
							$newupdateadmin["useradmin_oldpassword1"],
							$newupdateadmin["useradmin_oldpassword2"],
							$newupdateadmin["useradmin_oldpassword3"]
						);
					
		$id = $newuseradmin->getId();

	  	$data = $this->data->Updatepass($newuseradmin);
	  	if($data) $this->logcontrol->addLogLine("Passwd changed for Useradmin with ID=".$id, 3);
		else $this->logcontrol->addLogLine("Unable to change Passwd for Useradmin with ID=".$id, 3);
		
		return $data;
	  	
	}
	
	
	//
	//	Delete useradmin (same as Destroy())
	//
	function Delete($id)
	{
		return $this->Destroy($id);
	}

	
	//
	//	Destroy useradmin
	//
	function Destroy($id)
	{
		$data = $this->data->Destroy($id);
		if($data) $this->logcontrol->addLogLine("Useradmin with ID=".$id." removed", 3);
		else $this->logcontrol->addLogLine("Unable to remove Useradmin with ID=".$id, 3);
		
		return $data;
		
	}
	
	
	
	////////////////////////////////////////////
	///////  Authentication Methods //////////
	//////////////////////////////////////////
	
	function AuthenticateUseradmin($useradmin)
	{		
	  $data = $this->data->AuthenticateUseradmin($useradmin);

	  if($data) $this->logcontrol->addLogLine("Useradmin with login=".$useradmin->getLogin()." authenticated", 3);
	  else $this->logcontrol->addLogLine("Useradmin with login=".$useradmin->getLogin()." NOT authenticated", 3);
		
	  return $data;
	}
	
}



?>